﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Web.Controls {

    /// <summary>
    /// Obsługuje skalowalne obrazki. Obrazki muszą się znajdować w katalogu /Data/SiteImages. Obrazek może być wykorzystany tylko w sytuacji w której jego szerokość jest określona za pomocą pikseli.
    /// </summary>
    public class RwdSiteImage : Image {

        #region Fields

        private static bool useRewriteModule = false;
        private static bool useRewriteModule_Set = false;

        private const string URL = "/{0}.ashx?configuration={1}&width=320&height=1000&file={2}";
        private const string URL_REWRITED = "/{0}/{1}/320x1000/{2}";

        private const string HANDLER_FULL = "DynamicImageFileBased";
        private const string HANDLER_REWRITE = "fbdi";

        private string fileName;
        private string userCssClass = String.Empty;

        private Dictionary<RwdSiteImageVariant, string> configs;

        #endregion

        #region Properties

        public override string CssClass {
            get {
                return base.CssClass;
            }
            set {
                this.userCssClass = value;
                List<string> cssParts = new List<string>();
                if (!string.IsNullOrEmpty(this.userCssClass))
                    cssParts.Add(this.userCssClass);
                cssParts.Add("scaled-image");
                int scale = this.PercentScaling;
                if (scale <= 0 || scale > 100)
                    scale = 100;
                cssParts.Add("scaled-" + scale);
                base.CssClass = string.Join(" ", cssParts.ToArray());
            }
        }

        public int PercentScaling { get; set; }

        private RwdSiteImageVariant variant = RwdSiteImageVariant.Default;
        public RwdSiteImageVariant Variant {
            get { return this.variant; }
            set {
                if (this.variant != value) {
                    this.variant = value;
                    this.SetUrl();
                }
            }
        }

        public string FileName {
            set {
                this.fileName = value;
                this.SetUrl();
            }
            get {
                return this.fileName;
            }
        }

        private string UrlBase {
            get {
                if (!useRewriteModule_Set) {
                    useRewriteModule = HttpRuntime.UsingIntegratedPipeline;
                    useRewriteModule_Set = true;
                }
                return (useRewriteModule) ? URL_REWRITED : URL;
            }
        }

        #endregion

        #region Constructors

        public RwdSiteImage() {
            this.configs = new Dictionary<RwdSiteImageVariant, string>();
            this.configs.Add(RwdSiteImageVariant.Default, "SiteImages");
            this.configs.Add(RwdSiteImageVariant.Gray, "GraySiteImages");
            this.Variant = RwdSiteImageVariant.Default;
            this.CssClass = "";
        }

        #endregion

        #region Methods

        private void SetUrl() {
            this.ImageUrl = string.Format(UrlBase, (useRewriteModule) ? HANDLER_REWRITE : HANDLER_FULL, this.configs[this.Variant], FileName);
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            JQueryLibraries jql = new JQueryLibraries();
#if DEBUG
            jql.IncludeFileBasedOnJQuery("blackhouse.Web.Controls.RwdImg.js.js,blackhouse.Web.Controls");
#else
            jql.IncludeFileBasedOnJQuery("blackhouse.Web.Controls.RwdImg.js.min.js,blackhouse.Web.Controls");
#endif
        }

        #endregion

    }

    public enum RwdSiteImageVariant {
        Default,
        Gray
    }

}